package controls;

import java.awt.Dimension;

import java.text.ParseException;


import javax.swing.*;
import javax.swing.text.MaskFormatter;

import main.Application;

import tools.Pair;


public class DateFromTo extends JPanel
{
	public static final int EDIT_MODE = 0;
	public static final int SEARCH_MODE = 1;
	public static final int HEIGHT_FACTOR=4;
	public static final int WIDTH_FACTOR=9;
	private static final String emptyMask = "    -  -  ";
	
	static final long serialVersionUID = 1L;
	
	JTextField fromTB;
	JTextField toTB;
	JTextField simpleDateTB;
	
	JPanel searchPanel;
	JPanel editPanel;
	private int runMode; //jeden z finali
	private Dimension panelSize;
	
	public DateFromTo() throws ParseException
	{
		panelSize = new Dimension 
		(Application.getInstance().getFontSize()*WIDTH_FACTOR, 
				6+ (Application.getInstance().getFontSize())*HEIGHT_FACTOR );
		MaskFormatter formatter = new MaskFormatter("####-##-##");
		fromTB = new JFormattedTextField(formatter);
		toTB = new JFormattedTextField(formatter);
		simpleDateTB = new JFormattedTextField(formatter);
		searchPanel = new JPanel();
		editPanel = new JPanel();
		
//		fromTB.setLocation(0,0);
//		toTB.setLocation(10,20);
//		fromTB.setSize(40, 10);
		fromTB.setColumns(10);
//		toTB.setSize(40, 10);
		toTB.setColumns(10);
//		simpleDateTB.setSize(40, 10);
		simpleDateTB.setColumns(10);
		
		searchPanel.add(fromTB);
		searchPanel.add(toTB);
		
		editPanel.add(simpleDateTB);
		
		searchPanel.setLocation(0, 0);
		editPanel.setLocation(0, 0);		
		searchPanel.setSize(panelSize);
		editPanel.setSize(panelSize);		
		this.setSize(panelSize);
		this.setLayout(null);
		this.add(searchPanel);
		this.add(editPanel);
		
		
	}
	public void setRunMode(int runMode)
	{
		this.runMode = runMode;
		if(runMode == SEARCH_MODE)
		{
			searchPanel.setVisible(true);
			editPanel.setVisible(false);
		}
		else
		{
			searchPanel.setVisible(false);
			editPanel.setVisible(true);
		}
	}
	public void setDate(String date)
	{
		simpleDateTB.setText(date);
	}
	public Object getDate()
	{
		if(runMode==SEARCH_MODE)
		{
			if(fromTB.getText().equals(emptyMask) || toTB.getText().equals(emptyMask))
				return "";				
			return new Pair<String,String>(fromTB.getText(), toTB.getText());
		}
		else 
		{
			if(simpleDateTB.getText().equals(emptyMask))
				return "";
			return simpleDateTB.getText();
		}
	}	
	@Override
	public void setEnabled(boolean enabled)
	{
		super.setEnabled(enabled);
		simpleDateTB.setEnabled(enabled);
	}	
}
